package com.sh.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.sh.entity.SystemParam;

//数据的 工具类
public class DataUtil {

	/**
	 * 根据 id 与 parentId 完成树状结构数据的装配
	 * @author Administrator
	 * @param list<SystemParam>
	 * @return JSONOBject
	 * */
	public static SystemParam getTreeJsonObj(List<SystemParam> list){
		SystemParam root = new SystemParam();
		List<SystemParam> childList = new ArrayList<SystemParam>();

		Iterator<SystemParam> iterator = list.iterator();
		while(iterator.hasNext()){
			SystemParam param = iterator.next();
			if(0==param.getParentId()){
				childList.add(param);
				List<SystemParam> diGuiChildJson = getDiGuiChildJson(list,param);
				Collections.sort(diGuiChildJson);
				param.setChildList(diGuiChildJson);
			}
		}
		Collections.sort(childList);
		root.setChildList(childList);
		return root;
	}

	//递归拿取 子元素信息
	private static List<SystemParam> getDiGuiChildJson(List<SystemParam> list,SystemParam parentNode) {
		// TODO Auto-generated method stub 
		List<SystemParam> systemParamList = new ArrayList<SystemParam>();
		Iterator<SystemParam> iterator = list.iterator();
		while(iterator.hasNext()){
			SystemParam childNode = iterator.next();
			if(parentNode.getId()==childNode.getParentId()){
				systemParamList.add(childNode);
				List<SystemParam> diGuiChildJson = getDiGuiChildJson(list,childNode);
				Collections.sort(diGuiChildJson);
				childNode.setChildList(diGuiChildJson);
			}
		}

		return systemParamList;
	}








}
